package com.wnhoo.smartfishtank.brodcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by Administrator on 2016/3/25.
 */
public class NetWorkChangeBroadcastReceiver extends BroadcastReceiver {
    public final static String NETWORKCHANGE = "com.wnhoo.networkchange";
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager=
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager!=null) {
            NetworkInfo[] networkInfos=connectivityManager.getAllNetworkInfo();
            for (int i = 0; i < networkInfos.length; i++) {
                NetworkInfo.State state=networkInfos[i].getState();
                if (NetworkInfo.State.CONNECTED==state) {
                    Intent intent1 = new Intent(NETWORKCHANGE);
                    intent1.putExtra("isNetWork",true);
                    context.sendBroadcast(intent1);
                    return;
                }
            }
        }
        Intent intent1 = new Intent(NETWORKCHANGE);
        intent1.putExtra("isNetWork",false);
        context.sendBroadcast(intent1);
//
//        //没有执行return,则说明当前无网络连接
//        System.out.println("------------> Network is validate");
//        intent.setClass(context, NetWorkErrorActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//        context.startActivity(intent);
    }

}
